package cc.sheji.Service;


import cc.sheji.domain.*;
import cc.sheji.reposity.AdRepository;
import cc.sheji.util.QueryHelp;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.BiConsumer;


@Service
public class AdService {

    @Autowired
    AdRepository adRepository;

    public Ad create(Ad entity) {
        entity = adRepository.save(entity);
        return entity;
    }

    public Ad get(Long id) {
        Ad entity = adRepository.findById(id).get();
        return entity;
    }

    public boolean delete(long id){
        Ad ad = adRepository.findById(id).get();
        ad.setDeleted(1);
        adRepository.save(ad);
        return true;
    }

    public Ad update(Ad entity) {
        System.out.println(entity);
        entity = adRepository.save(entity);
        return entity;
    }

    public List<Ad> list(AdQueryCriteria criteria, Pageable pageable) {
        Page<Ad> page = adRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return page.getContent();
    }
}
